package sis.ra.founded;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import sis.ra.utility.Utils;


public class rprecision {

	public static HashMap <String, Double> getGroundTruth()
	{
		HashMap <String, Double> gth=new HashMap <String, Double> ();
		String filepath="C:\\Documents and Settings\\I820753\\Desktop\\dataset\\location_testing\\location.gth";
		String content=Utils.readFile(filepath);
//		System.out.println(content);
		String [] lines=content.split("\n");
		for (int i=0;i<lines.length;i++)
		{
			String term=lines[i].split("\t")[0]+"\t"+lines[i].split("\t")[1];
			Double score=Double.parseDouble(lines[i].split("\t")[2]);
	//		System.out.println(term+"=="+score);
			gth.put(term, score);
		}
		return gth;
	}
	public static void main(String[] args) {
		String filepath="C:\\Documents and Settings\\I820753\\Desktop\\dataset\\location_testing\\";//Amazon.com_wiki.score1";
		String evaluatefile=".score3";//0
		int totalnum=30;
		HashMap <String, Double> gth= getGroundTruth();
		ArrayList<String> ranks=new ArrayList<String> ();
		File filepaths=new File(filepath);
		File[] files=filepaths.listFiles();
		HashMap <String, Integer> companyallproducts=new HashMap <String, Integer> ();
		Iterator <String> it = gth.keySet().iterator();
		while (it.hasNext())
		{
			String line=it.next();
	//		System.out.println(line);
			String companyname=line.split("\t")[0];
			if (companyallproducts.containsKey(companyname))
			{
				Integer num=companyallproducts.get(companyname);
				num=num+1;
				companyallproducts.remove(companyname);
				companyallproducts.put(companyname, num);
			}
			else
			{
				companyallproducts.put(companyname, 1);
			}
		}
		
		Iterator <String> coms=companyallproducts.keySet().iterator();
		while (coms.hasNext())
		{
			String comname=coms.next();
	//		System.out.println(comname+"	"+companyallproducts.get(comname));
		}
		double avgrpre=0.0;
		for (int t=0;t<files.length;t++)
		{
			boolean old=true;
			double rprecision=0.0;
			if (!files[t].toString().contains(evaluatefile)) continue;
			if (files[t].toString().contains("Sunoco")) continue;
			if (files[t].toString().contains("Marathon Oil")) continue;
			if (files[t].toString().contains("Conocophillips")) continue;
			if (files[t].toString().contains("Frontier Oil")) continue;
			if (files[t].toString().contains("Murphy Oil")) continue;
			if (files[t].toString().contains("Valero Energy Corporation")) continue;
			
			if (files[t].toString().contains("AOL")) old=false;
			if (files[t].toString().contains("Ask.com")) old=false;
			if (files[t].toString().contains("AT&T")) old=false;
			if (files[t].toString().contains("Costco")) old=false;
			if (files[t].toString().contains("DELL")) old=false;
			if (files[t].toString().contains("IBM")) old=false;
			if (files[t].toString().contains("National Iranian Oil Company")) old=false;
			if (files[t].toString().contains("PetroChina")) old=false;
			if (files[t].toString().contains("Saudi Aramco")) old=false;
			if (files[t].toString().contains("Symantec")) old=false;
			
			String content=Utils.readFile(files[t].toString());
	//		System.out.println(content);
			
			String companyname=files[t].toString().substring(67).replace(evaluatefile, "").replace("_wiki", "");
	//		System.out.println(companyname);
			if (!companyallproducts.containsKey(companyname)) continue;
			Integer comnum=companyallproducts.get(companyname);
	
			String [] lines=content.split("\n");
			for (int i=0;i<lines.length;i++)
			{
				if (lines[i].length()<1) continue;
				String term=lines[i].split("\t")[0]+"\t"+lines[i].split("\t")[1];
			
				if (gth.containsKey(term))
				{
					Double s=gth.get(term)/i;
		//			System.out.println(comnum);
					if (i<comnum)
					{
						rprecision=rprecision+1;
					}
				}
			}
			
//			System.out.println(companyname+"\t"+rprecision+"\t"+comnum);
			double nrprecision=rprecision/comnum;
			System.out.println(companyname+"\t"+nrprecision+"\t"+rprecision+"\t"+comnum);
			
			avgrpre=avgrpre+nrprecision;
				
		}
		
		avgrpre=avgrpre/25;
		System.out.println("avg_rprecision"+"\t"+avgrpre);

	}
}
